<?php
	####################################################
	# NImg 이미지 리사이즈                             #
	####################################################

	# $retname = NImg::make_thumbs($_FILES[$fname], "./temp/img/", array(10,20,30,40,50,60,70,80,90,150,300));
	class NImg {

		# 섬네일 만들기
		public static function make_thumbs($pfiles_arr, $fname, $size_set_arr = array(50,100,150)) {
			Nlog::xmp($pfiles_arr);
			if (is_uploaded_file($pfiles_arr['tmp_name'])){
				if($_FILES['img']['error'] != 0){
					echo "error";
					return ;
				}

				$new_file = substr(md5(microtime()),0,16);

				foreach($size_set_arr as $key=>$val){
					if (preg_match("/^image/", $pfiles_arr['type'])) {
						NImg::thumbnail($pfiles_arr['tmp_name'], sprintf("%s%s_%d", $fname, $new_file, $val), $val, $val);
					}
				}

				return $new_file;
			} else {
				$ret = "not upload";
				return $ret;
			}
		}

		# 배열 주소로 썸네일 제작
		# $uldr = new Uploader();
		# $upfarr = $uldr->init($_FILES);
		# $make_thumbs = NImg::thumbarr($upfarr, "./test/", array(10,20,30));
		public static function thumbarr($srcarr, $fname, $size_set_arr = array(50,100,150)) {
			$ret = array();
			foreach($srcarr as $key=>$val){
				//Nlog::puts($val);
				$ret[$val][] = $fname.$val;
				foreach($size_set_arr as $k=>$v){
					//Nlog::puts($v);
					//echo sprintf("%s%s_%d", $fname, $val, $v);
					$fnamearr = IoFileAndDir::explode_file_and_file_ext($val);
					$file = $fnamearr['name'];
					$ext = $fnamearr['ext'];
					$folder = sprintf("%s%s_%d.%s", $fname, $file, $v, $ext);
					NImg::thumbnail($fname.$val, $folder, $v, $v);
					$ret[$val][] = $folder;
				}
			}

			return $ret;
		}

		# 썸네일 제작
		public static function thumbnail($src, $folder, $size_x=100, $size_y=100) {
			if (!is_file($src)) {
				echo "not image";
			}
			$size	= getimagesize($src);
			//Nlog::xmp($size);
			switch ($size[2]){
				case 1:	$image	= ImageCreatefromGif($src); break;
				case 2:	$image	= ImageCreatefromJpeg($src); break;
				case 3:	$image	= ImageCreatefromPng($src);  break;
			}

			$re_size	= NImg::img_size_set($src, $size_x, $size_y, $size[0], $size[1]);
			$g_width	= 0;
			$g_height	= 0;
			$new_size_x	= $re_size[0];
			$new_size_y	= $re_size[1];
			$dst		= ImageCreateTruecolor($new_size_x,$new_size_y);
			Imagecopyresampled($dst,$image,0,0,$g_width,$g_height,$new_size_x,$new_size_y,$size[0],$size[1]);

			ImageJpeg($dst,$folder,100);
			ImageDestroy($dst);
		}

		# 이미지 사이즈 조절
		private static function img_size_set($ImgName, $getWSize="", $getHSize="", $oriWSize="", $oriHSize=""){

			if($oriWSize != "" && $oriHSize != ""){
				$ImgSize[0]	= $oriWSize;
				$ImgSize[1]	= $oriHSize;
			}else{
				$ImgSize	= @getimagesize($ImgName);
			}

			if($getWSize&&$getHSize){
				$PreWidth	= $getWSize;
				$PreHeight	= $getHSize;
			}
			else {
				$PreWidth	= $ImgSize[0];
				$PreHeight	= $ImgSize[1];
			}

			if ($ImgSize[0] >= $PreWidth && $ImgSize[1] >= $PreHeight)
			{
				$height = $PreWidth * $ImgSize[1] / $ImgSize[0];
				$width = $PreHeight * $ImgSize[0] / $ImgSize[1];

				if($width >= $PreWidth && $height <= $PreHeight){
					$width = $PreWidth;
					$height = $width * $ImgSize[1] / $ImgSize[0];
				}
				if($width <= $PreWidth && $height >= $PreHeight){
					$height = $PreHeight;
					$width = $height * $ImgSize[0] / $ImgSize[1];
				}
			}else if ($ImgSize[0] >= $PreWidth || $ImgSize[1] >= $PreHeight){
				if($ImgSize[0] >= $PreWidth){
					$width = $PreWidth;
					$height = $width * $ImgSize[1] / $ImgSize[0];
				}
				if($ImgSize[1] >= $PreHeight){
					$height = $PreHeight;
					$width = $height * $ImgSize[0] / $ImgSize[1];
				}
			}else{
				$width = $ImgSize[0];
				$height = $ImgSize[1];
			}

			if(!$width || !$height){
				$width = $PreWidth;
				$height = $PreHeight;
			}

			$ReSizeImg = array($width,$height);

			return $ReSizeImg;
		}

	}
?>